package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.Travel;
import cn.wolfcode.luowowo.exception.LogicException;
import cn.wolfcode.luowowo.query.TravelQuery;
import cn.wolfcode.luowowo.repository.TravelRepository;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.ITravelService;
import cn.wolfcode.luowowo.service.IUserService;
import cn.wolfcode.luowowo.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

//旅游日记业务实现类
@Service
public class TravelServiceImpl implements ITravelService {
    //注入旅游日记数据接口
    @Autowired
    private TravelRepository travelRepository;
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IUserService userService;
    @Autowired
    private IDestinationService destinationService;

    //根据旅游日记id查询旅游日记信息
    @Override
    public Travel get(String id) {
        Travel travel = travelRepository.findById(id).orElse(null);
        //获取所属目的地，作者信息
        travel.setDest(destinationService.get(travel.getDestId()));
        travel.setAuthor(userService.get(travel.getUserId()));
        return travel;
    }

    //旅游日记分页查询
    @Override
    public Page<Travel> list(TravelQuery qo) {
        //创建条件对象
        Query query = new Query();
        //创建分页对象,设置排序条件
        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.Direction.DESC, qo.getOrder());
        //高查
        //默认只查询已经发布的
        if (qo.getState() != -1){
            query.addCriteria(Criteria.where("state").is(qo.getState()));
        }
        //是否要求某个目的地
        if (StringUtils.hasLength(qo.getDestId())){
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }
        //获取天数范围条件
        if (qo.getDayType() != -1){
            int[] dayRange = qo.getDayRange(qo.getDayType());
            query.addCriteria(Criteria.where("day").gte(dayRange[0]).lte(dayRange[1]));
        }
        //获取消费数范围条件
        if (qo.getPerExpendType() != -1){
            int[] expendRange = qo.getExpendRange(qo.getPerExpendType());
            query.addCriteria(Criteria.where("perExpend").gte(expendRange[0]).lte(expendRange[1]));
        }

        //调用数据库工具类查询
        Page<Travel> travels = DBHelper.PagingQuery(mongoTemplate, query, Travel.class, pageable);
        List<Travel> content = travels.getContent();
        //作者和目的地信息处理
        if (content != null && content.size()>0){
            for (Travel travel : content) {
                //获取作者
                travel.setAuthor(userService.get(travel.getUserId()));
                //获取目的地
                travel.setDest(destinationService.get(travel.getDestId()));
            }
        }
        return travels;
    }

    @Override
    public void save(Travel travel) {
        //防止调用者传了id覆盖了数据
        travel.setId(null);
        //冗余字段的处理
        Destination destination = destinationService.get(travel.getDestId());
        travel.setDestId(destination.getId());
        travel.setDestName(destination.getName());
        travel.setCreateTime(new Date());
        travelRepository.save(travel);
    }

    @Override
    public void update(Travel travel) {
        travelRepository.save(travel);
    }

    @Override
    public void changeState(String id, int state) {
        //查询要改状态的游记
        Travel travel = get(id);
        if (travel == null){
            throw new LogicException("非法请求,没有找到改游记");
        }
        if (travel.getState() == Travel.STATE_WAITING){
            //如果存在游记,状态是待审核的，则修改它的状态
            travel.setState(state);
            //记录更新时间
            travel.setLastUpdateTime(new Date());
            //修改
            this.update(travel);
        }else if (travel.getState() == Travel.STATE_RELEASE){
            //下架    处理已经发布的,清除发布时间
            travel.setState(state);
            travel.setLastUpdateTime(null);
            //记录更新时间
            travel.setLastUpdateTime(new Date());
            //修改
            this.update(travel);
        }
    }

    //目的地下点击量前三的游记
    @Override
    public List<Strategy> viewnumTop3(String destId) {
        //没有排除自己是前三     改成mongoTemplate查询和用Query对象过滤即可
        PageRequest pageable = PageRequest.of(0,3);
        List<Strategy> list = travelRepository.findByDestIdOrderByViewnumDesc(destId , pageable);
        return list;
    }
}
